package com.kingyea.mobilepolice.apimanage.config.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/** tomcat 监听器 */
@Component
public class TomcatListener implements ApplicationListener<WebServerInitializedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(TomcatListener.class);

    private int serverPort;

    public int getPort() {
        return this.serverPort;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
//        System.out.println("Get WebServer port { "+serverPort+" } !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        logger.info("Get WebServer port {} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",serverPort);
    }
}

////for 1.5.x  Spring Boot
//public class ServerConfig implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
//    private int serverPort;;
//
//    public int getPort() {
//        return this.serverPort;
//    }
//
//    @Override
//    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
//        this.serverPort = event.getEmbeddedServletContainer().getPort();
//    }
//}